package fuzzy;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import net.sourceforge.jFuzzyLogic.FIS;
import net.sourceforge.jFuzzyLogic.plot.JDialogFis;

/**
 * FUZZY - SI - BSI
 */
public class Main {
	
	private static String fileName = "class.fcl";
	private static String csvFile = "testes.csv";
	private static String cvsSplitBy = ";";
	
    public static void main(String[] args) throws Exception {
        FIS fis = FIS.load(fileName,true);
        
        if( fis == null ) { 
            System.err.println("Can't load file: '" + fileName + "'");
            return;
        }
        
        // Create a plot
     	JDialogFis jdf = new JDialogFis(fis, 800, 600);
        
    	BufferedReader br = null;
    	String line = "";
    	int erros = 0;
    	int registros = 0;
    	
    	try {
    		br = new BufferedReader(new FileReader(csvFile));
    		while ((line = br.readLine()) != null) {
    			String[] param = line.split(cvsSplitBy);
    			// Set inputs
    	        fis.setVariable("idade",Integer.parseInt(param[0])); 
    	        fis.setVariable("anoCirurgia", Integer.parseInt(param[1]));
    	        fis.setVariable("nodulos", Integer.parseInt(param[2]));
    	        fis.evaluate();

    	        int classe = Integer.parseInt(param[3]);
    	        
    	        System.out.println("Idade:"+fis.getVariable("idade").getValue()+
    	        				   " Ano_cirurgia:"+fis.getVariable("anoCirurgia").getValue()+
    	        				   " nodulos:"+fis.getVariable("nodulos").getValue()+
    	        				   " = Classe " + fis.getVariable("classe").getLatestDefuzzifiedValue() +" ("+ classe );
    	
    	        jdf.repaint();
    			Thread.sleep(100);
    	        
    	        if ((classe == 1 && fis.getVariable("classe").getValue() > 0.5) ||
    	        	(classe == 2 && fis.getVariable("classe").getValue() < 0.5) ) {
    	        	System.out.println("Erro!\n\n");
    	        	erros++;
    	        }
    	        registros++;
    		}
     
    	} catch (FileNotFoundException e) {
    		e.printStackTrace();
    	} catch (IOException e) {
    		e.printStackTrace();
    	} finally {
    		if (br != null) {
    			try {
    				br.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    		System.out.println("Erros:"+erros);
    		System.out.println("Total Registros:"+registros);
    		double pErros = (double) erros / registros;
    		System.out.println("Porcentagem de erros:"+ (pErros * 100) + "%");
    	}
        
        /*// Set inputs
        fis.setVariable("anoCirurgia", 69);
        fis.setVariable("idade", 55);
        fis.setVariable("nodulos", 22);*/

        // Evaluate
//        fis.evaluate();
        /*fis.getVariable("idade").chart(true);
        fis.getVariable("anoCirurgia").chart(true);
        fis.getVariable("nodulos").chart(true);
        fis.getVariable("classe").chart(true);*/
//        fis.getFunctionBlock("sobrevivencia").chart();
//
//        // Show output variable's chart 
//        //fis.getVariable("classe").chartDefuzzifier(true);
//        
//        System.out.println("Output value:" + fis.getVariable("classe").getValue()); // Show output variable
//        System.out.println("Output class:" + fis.getVariable("classe").getMembership("Classe1"));
//        System.out.println("Output class:" + fis.getVariable("classe").getMembership("Classe2"));
        //System.out.println("Output class:" + fis.getVariable("classe"). 

        // Print ruleSet
        //System.out.println(fis);
    }
}